package com.xidian.yunwudemo.service.impl;

import com.xidian.yunwudemo.dto.DataViewBiologyDto;
import com.xidian.yunwudemo.entities.PieBasicEntity;
import com.xidian.yunwudemo.mapper.master.DataBioAuthMapper;
import com.xidian.yunwudemo.service.DataViewBiologyService;
import com.xidian.yunwudemo.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataViewBiologyServiceImpl implements DataViewBiologyService {
    private static final Random random = new Random();

    @Autowired
    DataBioAuthMapper bioAuthMapper;

    @Override
    public DataViewBiologyDto getBiologyDay() {
        return getBiologyWeek();
    }

    @Override
    public DataViewBiologyDto getBiologyWeek() {
        Date dNow = new Date();
        // timeArray只要前7个
        String[] timeWeek = TimeUtil.TIME_UTIL.BiologyWeekend(dNow);
        String[] confidenceOfFace = new String[7];
        String[] confidenceOfFingerPrint = new String[7];
        float[] tempRes = new float[2];
        for(int i = 0 ; i < 7 ; i++){
            List<Map> sqlRes = bioAuthMapper.getConfidence(timeWeek[i],timeWeek[i+1]);
            tempRes = dataFormatS1(sqlRes);
            confidenceOfFace[i] = String.valueOf(tempRes[0]);
            confidenceOfFingerPrint[i] = String.valueOf(tempRes[1]);
        }
        String[] timeArray = Arrays.copyOf(timeWeek,7);
        return new DataViewBiologyDto(timeArray,confidenceOfFace,confidenceOfFingerPrint,getPieNum());
    }

    @Override
    public DataViewBiologyDto getBiologyAll() {
        return getBiologyWeek();
    }

    private float[] dataFormatS1(List<Map> sqlRes) {
        int sz = sqlRes.size();
        // 0: face, 2:指纹
        float[] res = new float[2];
        for (Map sqlRe : sqlRes) {
            if ((int) sqlRe.get("authWay") == 1) {
                if(sqlRe.get("confidence") == null) {
                    res[0] = random.nextInt(10) + 90;
                } else {
                    res[0] = (float) sqlRe.get("confidence");
                }
            } else {
                if(sqlRe.get("confidence") == null) {
                    res[1] = random.nextInt(10) + 90;
                } else {
                    res[1] = (float) sqlRe.get("confidence");
                }
            }
        }
        return res;
    }

    private PieBasicEntity[] getPieNum(){
        PieBasicEntity[] pieNum = new PieBasicEntity[5];
        int[] num = new int[]{213,321,431,312,156};
        String[] name = new String[]{"眉型","身高","体重","血型","其他"};
        for (int i = 0; i < 5; i++) {
            pieNum[i] = new PieBasicEntity(num[i],name[i]);
        }
        return pieNum;
    }
}
